package com.syyo.admin.controller;


import com.syyo.admin.domain.ResultVo;
import com.syyo.admin.domain.excel.UserExport;
import com.syyo.admin.domain.req.UserReq;
import com.syyo.admin.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wang
 * @since 2020-07-17
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class SysUserController {


    @Autowired
    private SysUserService userService;

    /**
     * 用户新增
     * @param req
     * @return
     */
    @PreAuthorize("@el.check('system:user:add')")
    @PostMapping
    public ResultVo add(@RequestBody UserReq req){
        return userService.add(req);
    }

    /**
     * 用户删除
     * @param req
     * @return
     */
    @PreAuthorize("@el.check('system:user:del')")
    @DeleteMapping
    public ResultVo del(@RequestBody UserReq req){
        return userService.del(req);
    }

    /**
     * 用户修改
     * @param req
     * @return
     */
    @PreAuthorize("@el.check('system:user:edit')")
    @PutMapping
    public ResultVo edit(@RequestBody UserReq req){
        return userService.edit(req);
    }


    /**
     * 用户详情
     * @param
     * @return
     */
    @GetMapping("/{id}")
    public ResultVo findOne(@PathVariable("id")Integer id){
        return userService.findOne(id);
    }

    /**
     * 用户列表
     * @param req
     * @return
     */
    @GetMapping("/list/{pageNum}/{pageSize}")
    public ResultVo findAll(@PathVariable("pageNum") Integer pageNum,
                            @PathVariable("pageSize") Integer pageSize,
                            UserReq req){
        return userService.findAll(pageNum,pageSize,req);
    }


    @PreAuthorize("@el.check('system:user:import')")
    @PostMapping("/import")
    public ResultVo importUser(MultipartFile file) throws IOException {
        return userService.importUser(file);
    }

    @PreAuthorize("@el.check('system:user:export')")
    @GetMapping("/export")
    public ResultVo exportUser(UserExport userExport){
        return userService.exportUser(userExport);
    }

    /**
     * 用户修改密码
     * @param req
     * @return
     */
    @PutMapping("/pwd")
    public ResultVo editPwd(@RequestBody UserReq req){
        return userService.editPwd(req);
    }

    /**
     * 用户修改头像
     * @param
     * @return
     */
    @PostMapping("/avatar")
    public ResultVo editAvatar(MultipartFile file){
        return userService.editAvatar(file);
    }
}
